var seqNo = 0;//题号
var count = 0;//统计个数
var item =0;//题目项
var descriptions = "";//题目描述
var scoreList = new Array(new Array(), new Array());//标准分数字
var list = new Array(new Array(), new Array());//题目数字
var flag = "";
var isSave=0;//判断是否已经保存文件题目
/**
 * 动态添加心理健康调查题目
 */
function addQuestions() {
	seqNo += 1;
	if(isSave==0){
		$("#massage").html("");
		$("#massage").append(
				"<span style='color:red'>" + "请保存上一题后再添加!"
						+ "</span>");
		if(1==$("#flagId").val()){
			$("#massage").html("");
			questions(seqNo+$("#question").find("fieldset").size());
		}
		//seqNo = seqNo-1;
	}else{
		if("edit"==flag){
			$("#massage").html("");
			if(seqNo>$("#question").find("fieldset").size()){
				questions(seqNo);
			}else{
				questions($("#question").find("fieldset").size()+1);
			}
		}else{
			$("#massage").html("");
			$("#addQuestion").html();
			questions(seqNo);
		}
	}
}

// 动态添加心理健康调查题目的页面元素
function questions(s) {
	seqNo=s;
	return $("#addQuestion").append(
					"<div  style='margin:0 0px;width:90%'id='fieldset"
							+ seqNo
							+ "'>"
							+ "<fieldset><legend align='top'><a href='#'>编辑</a>/<a href='#?"
							+ seqNo
							+ "' onclick='removeSeqNo("
							+ seqNo
							+ ")'>删除</a></legend>"
							+ "<div><label id='seqNo"
							+ seqNo
							+ "'>"
							+ seqNo
							+ "</label>"
							+ "<textarea rows='' cols='' style='width:90%' name='descriptions' id='descriptionsId'>"
							+ "</textarea>"
							+ "</div>"
							+ "<div style='float:left;margin:0 0px;'>"
							+ "<lable>当前类型:</lable>"
							+ "<select id='type' onchange='select()' style='width:100px'>"
							+ "<option value='0' id='dx'>单选</option>"
							+ "<option  value='1' id='kf'>开放</option></select>"
							+ "</div>"
							+ "<input type='button' id='bc' onclick=\"saveQuestion("
							+ seqNo
							+ ","+$("#type").val()+")\" value='保存' style='width:100px;float:right;margin:0 30px;'/>"
							+ "<input type='button' id='xz' value='新增选项'  onclick='addAnswer("+seqNo+")' style='width:100px;float:right;margin:0 2px;'/>"
							+ "<div id='item'></div>" + "<div id=''></div>"
							+ "</fieldset>" + "</div>"
			);
}
/**
 * 动态添加结果项
 */
function addItem() {
	alert();
	item +=1;
	$("#itemId").append(
					 "<div style=\"width:90%;float:left;margin:0 15px;\" id='idItem"+item+"'>"
								+ "<label>总分下限:</label>"
								+ "<input type='text' name='' id='lowerLimit"+item+"' size=\"10\"></input>"
								+ "&nbsp;&nbsp;&nbsp;<label>总分上限:</label>"
								+ "<input type='text' name='' id='upperLimit"+item+"' size=\"10\" ></input>"
								+ "&nbsp;<label>显示结果:</label>"
								+ "<input type='text' name='' id='showResult"+item+"' value=''></input>"
								+"<input type='button' id='sc' value='删除'  onclick='removeAnswer("+item+")'style='width:60px;margin:0 20px;'/>"
								+ "</div>" );
}

//保存答案并且置为不可编辑状态
function saveAnswer(){
	$("#answerArea").find("input").each(function(){
	      this.disabled=true;
	   });
}

//修改答案且把答案区置为可编辑状态
function editAnswer(){
	$("#answerArea").find("input").each(function(){
	      this.disabled=false;
	   });
}
//删除答案得分情况
function removeAnswer(s){
	$("#idItem"+s).remove();
	
}
// 保存开放类型的问题项
function kfQuestion(seqNo, type, descriptions) {
	$("#question").append(
					"<div style='margin:0 0px;width:90%' id='fieldset"
							+ seqNo
							+ "'>"
							+ "<fieldset><legend align='top'><a href='#' onclick='editQuestion("
							+ seqNo
							+ ","
							+ type
							+ ")'>编辑</a>/<a href=\"#?"
							+ seqNo
							+ "\" onclick=\"removeSeqNo("
							+ seqNo
							+ ")\">删除</a></legend>"
							+ "<div><label id='seqNo"
							+ seqNo
							+ "'>"
							+ seqNo
							+ "</label><label id='subject"
							+ seqNo
							+ "'>"
							+ descriptions
							+ "</label><textarea rows='' cols='' style=\"width:89%\" id='des'>"
							+ "</textarea></div>" + "</fieldset>" + "</div>"

			);
}
// 保存单选类型的问题项
function dxQuestion(seqNo, type, descriptions) {
	$("#question")
			.append(
					"<div style='margin:0 0px;width:90%' id='fieldset"
							+ seqNo
							+ "'>"
							+ "<fieldset><legend align='top'><a href='#' onclick='editQuestion("
							+ seqNo + "," + type + ")'>编辑</a>/<a href=\"#?"
							+ seqNo + "\" onclick=\"removeSeqNo(" + seqNo
							+ ")\">删除</a></legend>" + "<div><label id='seqNo"
							+ seqNo + "'>" + seqNo
							+ "</label><label id='subject" + seqNo + "'>"
							+ descriptions + "</label>" + "</div>" + "<br>"
							+ "<div id='answerId" + seqNo + "'>" + "</div>"
							+ "</fieldset>" + "</div>");
}

// 保存新增选项作为单选类型的问题项
function dxAnswer(seqNo) {
	for ( var i = 1; i <= count; i++) {
		$("#answerId" + seqNo).append(
				"<label id='qItemId" + seqNo + i + "'>" + list[seqNo][i]
						+ "</label>" + "<input type='radio' id='answer" + seqNo
						+ i + "' name='' value='" + scoreList[seqNo][i]
						+ "'>&nbsp;&nbsp;&nbsp;");
	}
	;
}
/**
 * 保存新增问题
 */
function saveQuestion(seqNo, type) {
	isSave=1;
	list[seqNo] = new Array();
	scoreList[seqNo] = new Array();
	var type = $("#type").val();
	$("#massage").html("");
	descriptions = $("#descriptionsId").val();
	if (0 == $("#type").val()) {
		for ( var i = 1; i <= count; i++) {
			list[seqNo][i] = $("#questionItem" + i).val();
			scoreList[seqNo][i] = $("#score" + i).val();
	
		}
		$("#lowerLimit").val(0);
		if ("" == descriptions || 0 == count) {
			$("#massage").html("");
			$("#massage").append(
					"<span style='color:red'>" + "你当前选择的类型至少需添加一个问题项!"
							+ "</span>");
		} else {
			if ("" == flag) {
				dxQuestion(seqNo, type, descriptions);
				dxAnswer(seqNo);
				count = 0;
				$("#addQuestion").html("");
			} else {
				$("#fieldset" + seqNo).remove();
			
						dxQuestion(seqNo, type, descriptions);
				dxAnswer(seqNo);
				count = 0;
			}
		}
	} else if (1 == $("#type").val()) {
	
		if ("" == flag) {
			$("#fieldset" + seqNo).remove();
			kfQuestion(seqNo, type, descriptions);
			$("#addQuestion").html("");
		} else {
			if(seqNo<$("#question").find("fieldset").size()){
				$("#fieldset" + seqNo).replaceWith(kfQuestion(seqNo, type, descriptions));
				$("#fieldset" + seqNo).insertBefore($("#fieldset" + (seqNo+1)));
			}else{
				$("#fieldset" + seqNo).remove();
				
						kfQuestion(seqNo, type, descriptions);
			}

		}
	}
}
/**
 * 删除题目并使当前题号后面的题号数自动减一
 */
function removeSeqNo(s) {
	$("#fieldset" + s).remove();
	if (s < seqNo) {
	}
	for ( var i = s; i < seqNo; i++) {
		$("#seqNo" + (i + 1)).text(i);
	}
	seqNo = seqNo - 1;
}

/**
 * 编辑问卷题目
 */
function editQuestion(seqNo, type) {
	flag = "edit";
	var list = new Array(new Array(), new Array());
	list[seqNo]=new Array();
	var numbers = $("#answerId" + seqNo).find("input:radio").length;
		for(var j=1;j<=$("#answerId"+seqNo).find("label").length;j++){
			list[seqNo][j]=$("#answerId"+seqNo).find("label").eq(j-1).text();
	}
	var descriptionsName = $("#subject" + seqNo).text();
	if(seqNo<$("#question").find("fieldset").size()){
		$("#fieldset" + seqNo).replaceWith(questions(seqNo));
	}else{
		$("#fieldset" + seqNo).remove();
		$("#fieldset" + seqNo).replaceWith(questions(seqNo));
	}

	$("#descriptionsId").val(descriptionsName);
	if (1 == type) {
		$("#kf").attr("selected", "selected");
		$("#xz").hide();
	} else if (0 == type) {
		$("#dx").attr("selected", "selected");
		for ( var i = 1; i <= numbers; i++) {
			addAnswer();
			$("#questionItem" + i).val(list[seqNo][i]);
			if(1==$("#flagId").val()){
			$("#score" + i).val(2);
			}
			else{
				$("#score" + i).val(scoreList[seqNo][i]);
			}
			$("#no"+i).val(seqNo+1);
		}
		
	}
	list.clear();
}

/**
 * 动态添加单选项
 */
function addAnswer(no) {
	$("#massage").html("");
	count += 1;
	if (count <= 4) {
		$("#item").append(
						"<div style='width:80%;float:left;margin:0 0px;' id='itemAnswer"
								+ count
								+ "'>"
								+ "<label>选项:</label>"
								+ "<input type='text' name='' id='questionItem"
								+ count
								+ "' size='8'></input>"
								+ "&nbsp;&nbsp;&nbsp;<label>分数:</label>"
								+ "<input type='text' name='' id='score"
								+ count
								+ "' size='8' ></input>"
								+ "&nbsp;<label>跳转</label>"
								+ "<input type='text' name='' id='no"+ count
								+"' value='"+(no+1)+"' style='width:20px;'></input>"
								+ "<label>题</label>"
								+ "<input type='button' id='sc' value='删除'  onclick='removeItem("
								+ count
								+ ")' style='width:60px;margin:0 20px;'/>"
								+ "</div>");
		if (count <= 1) {
			$("#sc").hide();
		}
	}
}
// 删除选项
function removeItem(s) {
	$("#itemAnswer" + s).remove();
	count=s-1;
}
// 选择不同的下拉列表值显示不同的控件
function select() {
	if (0 == $("#type").val()) {
		$("#xz").show();
	} else if (1 == $("#type").val()) {
		$("#xz").hide();
		$("#item").html("");
	}
}

// 保存所有的问卷题到数据库
function save() {
	var json = "";
	var question="";
	var resultMassge="";
	for ( var i = 1; i <= seqNo; i++) {
		if ($("#answerId" + i).find("label").length > 0) {
			for ( var j = 1; j <= $("#answerId" + i).find("label").length; j++) {
				s=j==$("#answerId" + i).find("label").length?"":",";
				question +=[$("#qItemId" + i + j).text(),$("#answer" + i + j).val()]+s;	
			}
			json += [0,question,i,$("#subject" + i).text()]+":";
			question="";
		} else {
			if($("#subject" + i).text()!=""){
				json += [1,i,$("#subject" + i).text()]+":";
			}
			
		}
	}
	json = json.substr(0,json.length-1);
	$("#jsonId").val(json);
	for(var i=1;i<=$("#idItem").find("div").length;i++){
		resultMassge+=[$("#lowerLimit"+i).val(),$("#upperLimit"+i).val(),$("#showResult"+i).val()]+":";
	}
	resultMassge=resultMassge.substr(0,resultMassge.length-1);
    $("#judgmentting").val(resultMassge);

}

